package com.wzz.seckillone.utils;

import org.apache.commons.codec.digest.DigestUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

public class MD5Util {
    //实现加密
    public static String md5(String src){
        return DigestUtils.md5Hex(src);
    }

    //生成5位随机盐
    public static String getSalt(){
        String catalog = "1234567890qwertyuioplkjhgfdsazxcvbnm,./;'[]=-<>?:{}+_";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            sb.append(catalog.charAt(new Random().nextInt(catalog.length())));
        }
        return sb.toString();
    }

    //第一次加密所用的盐
    private static final String salt="1a2b3c4d";
    //第一次机密 inputPass  并返回
    public static String inputPassToFromPass(String inputPass){
        //不全用salt 用以混淆
        String str="" +salt.charAt(0)+salt.charAt(2)+inputPass+salt.charAt(5)+salt.charAt(4);
        return md5(str);
    }
    //第二次加密
    public static String formPassToDbPass(String fromPass,String salt){
        String src="" +salt.charAt(0)+salt.charAt(3)+fromPass+salt.charAt(5)+salt.charAt(4);
        return md5(src);
    }
    //Inoutpasstodbpass
    public static String inputPassToDbPass(String inputPass,String salt){
        String fromPass = inputPassToFromPass(inputPass);
        String dbPass = formPassToDbPass(fromPass, salt);
        return dbPass;
    }

    public static void main(String[] args) {
        //d38181b9b07e3173926228f93f0522bf
        /*String fromPass = inputPassToFromPass("123456");
        System.out.println(fromPass);

        System.out.println(formPassToDbPass("d3b1294a61a07da9b49b6e22b2cbd7f9", "1a2b3c4d"));
        System.out.println(inputPassToDbPass("18861383983","1a2b3c4d"));*/
        /*System.out.println(inputPassToFromPass("123456"));
        System.out.println(inputPassToDbPass("123456","1a2b3c4d"));*/
        /*
        //f30336ffcc3cedd6d266898adf84c225
        String dbPass = formPassToDbPass(fromPass, "1a2b3c4d");
        System.out.println(dbPass);
        System.out.println(inputPassToDbPass("123456", "1a2b3c4d"));*/
        System.out.println(md5("123456" + "g{<{+"));


    }
}
